package View;


import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JSlider;
import javax.swing.JButton;

import java.awt.Dimension;

import Presenter.ConfigScreenPresenter;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Choice;

/**
 * @author Ikenna
 *
 */
/**
 * @author Ikenna
 *
 */
/**
 * @author Ikenna
 *
 */
/**
 * @author Ikenna
 *
 */
/**
 * @author Ikenna
 *
 */
public class ConfigScreenView extends JPanel {
	/**
	 * 
	 */
	private final JTextField nameField;

	/**
	 * 
	 */
	private int pilotCurrentValue;
	
	/**
	 * 
	 */
	private int traderCurrentValue;
	
	/**
	 * 
	 */
	private int engineerCurrentValue;
	
	/**
	 * 
	 */
	private int fighterCurrentValue;
	
	/**
	 * 
	 */
	private final ConfigScreenPresenter presenter = new ConfigScreenPresenter();
	
	/**
	 * 
	 */
	private final JLabel pilotLabel;
	
	/**
	 * 
	 */
	private final JLabel traderLabel;
	
	/**
	 * 
	 */
	private final JLabel engineerLabel;
	
	/**
	 * 
	 */
	private final JLabel fighterLabel;
	
	/**
	 * 
	 */
	private final JSlider pilotSlider;
	
	/**
	 * 
	 */
	private final JSlider engineerSlider;
	
	/**
	 * 
	 */
	private final JSlider traderSlider;
	
	/**
	 * 
	 */
	private final JSlider fighterSlider;
	
	/**
	 * 
	 */
	private final JLabel availLabel;
	
	/**
	 * 
	 */
	private final JLabel usedLabel;
	
	/**
	 * 
	 */
	private final JButton confirmButton;

	/**
	 * 
	 */
	private final  JLabel lblGender;
	
	/**
	 * 
	 */
	private final JLabel lblDifficulty;
	
	/**
	 * 
	 */
	private final  Choice choice;
	
	private final Choice choice_1;

	/**
	 * Create the panel.
	 */
	public ConfigScreenView() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		final JPanel panel = new JPanel();
		add(panel);
		panel.setPreferredSize(new Dimension(550, 400));
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(23dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(39dlu;default):grow"),}));
		
		lblDifficulty = new JLabel("Difficulty");
		panel.add(lblDifficulty, "2, 2");
		
		choice_1 = new Choice();
		panel.add(choice_1, "12, 2");
		choice_1.add("");
		choice_1.add("Beginner");
		choice_1.add("Easy");
		choice_1.add("Normal");
		choice_1.add("Hard");
		choice_1.add("Impossible");
		
		choice_1.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				check();	
			}
			
		});
		
		lblGender = new JLabel("Gender");
		panel.add(lblGender, "2, 4");

		choice = new Choice();
		panel.add(choice, "12, 4");
		choice.add("");
		choice.add("Male");
		choice.add("Female");
		
		choice.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				check();	
			}
			
		});
		
		final JLabel lblPilot = new JLabel("Pilot");
		panel.add(lblPilot, "2, 6");
		
		pilotSlider = new JSlider();
		pilotSlider.setValue(0);
		pilotCurrentValue = pilotSlider.getValue();
		pilotSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				final int value = ((JSlider) arg0.getSource()).getValue();
				if(value > pilotCurrentValue){
					((JSlider) arg0.getSource()).setValue(presenter.changePilotLevel(1));
				}
				else if(value < pilotCurrentValue){
					((JSlider) arg0.getSource()).setValue(presenter.changePilotLevel(-1));
				}
				pilotCurrentValue = ((JSlider) arg0.getSource()).getValue();
				
				if(pilotLabel != null){
					pilotLabel.setText("" + ((JSlider) arg0.getSource()).getValue());
				}
				
				if(availLabel != null){
					availLabel.setText("" + presenter.getTotalAvailable());
				}
				
				if(usedLabel != null){
					usedLabel.setText("" + presenter.getUsedPoints());
				}
				
				check();
			}
		});
		pilotSlider.setMaximum(16);
		panel.add(pilotSlider, "12, 6");
		
		pilotLabel = new JLabel("4");
		pilotLabel.setText("" + pilotSlider.getValue());
		panel.add(pilotLabel, "14, 6");
		
		final JLabel lblEngineer = new JLabel("Engineer");
		panel.add(lblEngineer, "2, 8");
		
		engineerSlider = new JSlider();
		engineerSlider.setValue(0);
		engineerSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				final int value = ((JSlider) e.getSource()).getValue();
				if(value > engineerCurrentValue){
					((JSlider) e.getSource()).setValue(presenter.changeEngineerLevel(1));
				}
				else if(value < engineerCurrentValue){
					((JSlider) e.getSource()).setValue(presenter.changeEngineerLevel(-1));
				}
				engineerCurrentValue = ((JSlider) e.getSource()).getValue();
				
				if(engineerLabel != null){
					engineerLabel.setText("" + ((JSlider) e.getSource()).getValue());
				}
				
				if(availLabel != null){
					availLabel.setText("" + presenter.getTotalAvailable());
				}
				
				if(usedLabel != null){
					usedLabel.setText("" + presenter.getUsedPoints());
				}
				
				check();
			}
		});
		engineerSlider.setMaximum(16);
		engineerCurrentValue = engineerSlider.getValue();
		panel.add(engineerSlider, "12, 8");
		
		engineerLabel = new JLabel("4");
		engineerLabel.setText("" + engineerSlider.getValue());
		panel.add(engineerLabel, "14, 8");
		
		final JLabel lblNewLabel_1 = new JLabel("Trader");
		panel.add(lblNewLabel_1, "2, 10");
		
		traderSlider = new JSlider();
		traderSlider.setValue(0);
		traderSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				final int value = ((JSlider) arg0.getSource()).getValue();
				if(value > traderCurrentValue){
					((JSlider) arg0.getSource()).setValue(presenter.changeTraderLevel(1));
				}
				else if(value < traderCurrentValue){
					((JSlider) arg0.getSource())
					    .setValue(presenter.changeTraderLevel(-1));
				}
				traderCurrentValue = ((JSlider) arg0.getSource()).getValue();
				
				if(traderLabel != null){
					traderLabel.setText("" + ((JSlider) arg0.getSource()).getValue());
				}
				
				if(availLabel != null){
					availLabel.setText("" + presenter.getTotalAvailable());
				}
				
				if(usedLabel != null){
					usedLabel.setText("" + presenter.getUsedPoints());
				}
				
				check();
			}
		});
		traderSlider.setMaximum(16);
		panel.add(traderSlider, "12, 10");
		
		traderLabel = new JLabel("40");
		traderLabel.setText("" + traderSlider.getValue());
		panel.add(traderLabel, "14, 10");
		
		final JLabel lblNewLabel = new JLabel("Fighter");
		panel.add(lblNewLabel, "2, 12");
		
		fighterSlider = new JSlider();
		fighterSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				final int value = ((JSlider) e.getSource()).getValue();
				if(value > fighterCurrentValue){
					((JSlider) e.getSource()).setValue(presenter.changeFighterLevel(1));
				}
				else if(value < fighterCurrentValue){
					((JSlider) e.getSource()).setValue(presenter.changeFighterLevel(-1));
				}
				fighterCurrentValue = ((JSlider) e.getSource()).getValue();
				
				if(fighterLabel != null){
					fighterLabel.setText("" + ((JSlider) e.getSource()).getValue());
				}
				
				if(availLabel != null){
					availLabel.setText("" + presenter.getTotalAvailable());
				}
				
				if(usedLabel != null){
					usedLabel.setText("" + presenter.getUsedPoints());
				}
				
				check();
			}
		});
		fighterSlider.setValue(0);
		fighterCurrentValue = fighterSlider.getValue();
		fighterSlider.setMaximum(16);
		panel.add(fighterSlider, "12, 12");
		
		fighterLabel = new JLabel("4");
		fighterLabel.setText("" + fighterSlider.getValue());
		panel.add(fighterLabel, "14, 12");
		
		final JLabel lblAvailablePoints = new JLabel("Available Points");
		panel.add(lblAvailablePoints, "2, 14");
		
		availLabel = new JLabel("16");
		panel.add(availLabel, "4, 14");
		
		final JLabel lblUsedPoints = new JLabel("Used Points");
		panel.add(lblUsedPoints, "2, 16");
		
		usedLabel = new JLabel("0");
		panel.add(usedLabel, "4, 16");
		
		final JPanel panel_1 = new JPanel();
		panel.add(panel_1, "12, 18, fill, fill");
		
		final JLabel lblEnterName = new JLabel("Enter Name");
		panel_1.add(lblEnterName);
		
		nameField = new JTextField();
		nameField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				check();
				presenter.setName(nameField.getText());
			}
		});
		panel_1.add(nameField);
		nameField.setColumns(10);
		
		final JPanel panel_2 = new JPanel();
		panel.add(panel_2, "12, 20, fill, fill");
		
		final JButton btnNewButton = new JButton("Reset");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pilotSlider.setValue(0);
				engineerSlider.setValue(0);
				fighterSlider.setValue(0);
				traderSlider.setValue(0);
				presenter.reset();
			}
		});
		panel_2.add(btnNewButton);
		
		confirmButton = new JButton("Confirm");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter.confirm();
			}
		});
		confirmButton.setEnabled(false);
		panel_2.add(confirmButton);
	}

	/**
	 *  Method to check if the points have all been assigned
	 */
	public void check() {
		if(presenter.getTotalAvailable() == 0 && nameField != null && !nameField
		        .getText().equals("") && !choice.getSelectedItem().equals("") 
		        && !choice_1.getSelectedItem().equals("")) {
			if(confirmButton != null){
				confirmButton.setEnabled(true);
			}
		}
		else {
			if(confirmButton != null){
				confirmButton.setEnabled(false);
			}
		}
	}
	
	/**Getter
	 * @return getter
	 */
	public JSlider getPilotSlider() {
		return pilotSlider;
	}
	
	/**Getter
	 * @return getter
	 */
	
	public JSlider getEngineerSlider() {
		return engineerSlider;
	}
	
	/**Getter
	 * @return getter
	 */
	public JSlider getTraderSlider() {
		return traderSlider;
	}
	
	/**Getter
	 * @return getter
	 */
	public JSlider getFighterSlider() {
		return fighterSlider;
	}
	
	/**Getter
	 * @return getter
	 */
	public JTextField getNameField() {
		return nameField;
	}
	
	/**Getter
	 * @return getter
	 */
	public JButton getConfirmButton() {
		return confirmButton;
	}
}
